package com.mark.web.sys.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mark.common.ServerResponse;
import com.mark.web.sys.entity.from.DeptFrom;
import com.mark.web.sys.entity.from.DeptUpdateFrom;
import com.mark.web.sys.entity.po.Department;
import com.mark.web.sys.entity.vo.DeptVo;
import com.mark.web.sys.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description: 系统部门控制器
 * @Author: Mark
 * @CreateDate: 2019/1/13 15:00
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "系统-后台-部门模块")
@RestController
@RequestMapping("/sys/dept/")
public class DeptController {
    @Autowired
    private IDeptService iDeptService;

    @ApiOperation(value = "添加部门")
    @PostMapping("add.do")
    ServerResponse<String> addDept(@Valid DeptFrom deptFrom, BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Department department = new Department();
        BeanUtils.copyProperties(deptFrom,department);
        return iDeptService.addDept(department);
    }

    @ApiOperation(value = "编辑部门")
    @PostMapping("edit.do")
    ServerResponse<String> editDept(@Valid DeptUpdateFrom deptUpdateFrom,BindingResult result){
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Department department = new Department();
        BeanUtils.copyProperties(deptUpdateFrom,department);
        return iDeptService.editDept(department);
    }

    @ApiOperation(value = "获取部门信息")
    @ApiImplicitParam(name = "id",value = "部门ID",defaultValue = "1")
    @PostMapping(value = "getDept.do")
    public ServerResponse<DeptVo> getDept(Integer id){
        if(id == null){
            return ServerResponse.createByIllegalArgument("输入的部门ID不能为为空！");
        }
        return iDeptService.getDept(id);
    }

    @ApiOperation(value = "获取所有的部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "4")
    })
    @PostMapping(value = "getDeptList.do")
    public ServerResponse<PageInfo<DeptVo>> getDeptList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "4") int pageSize){
        return iDeptService.getDeptList(pageNum,pageSize);
    }
}
